package deltax.common
{

    public class StringDataParser extends Object
    {
        private var m_textData:String;
        private var m_parseIndex:int;
        private var m_reachedEOF:Boolean;
        private var m_line:int;
        private var m_charLineIndex:int;

        public function StringDataParser(param1:String = "")
        {
            this.m_textData = param1;
            return;
        }// end function

        public function get reachedEOF() : Boolean
        {
            return this.m_reachedEOF;
        }// end function

        public function set textData(param1:String) : void
        {
            this.m_textData = param1;
            this.reset();
            return;
        }// end function

        public function reset() : void
        {
            var _loc_1:int = 0;
            this.m_parseIndex = 0;
            var _loc_1:* = _loc_1;
            this.m_line = _loc_1;
            this.m_charLineIndex = _loc_1;
            this.m_reachedEOF = false;
            return;
        }// end function

        public function getLine() : String
        {
            var _loc_1:String = null;
            var _loc_2:String = "";
            while (!this.m_reachedEOF)
            {
                
                _loc_1 = this.getNextChar();
                if (_loc_1 == "\r" || _loc_1 == "\n")
                {
                    this.skipWhiteSpace();
                    if (_loc_2 != "")
                    {
                        return _loc_2;
                    }
                    continue;
                }
                _loc_2 = _loc_2 + _loc_1;
            }
            return _loc_2;
        }// end function

        public function getNextToken() : String
        {
            var _loc_1:String = null;
            var _loc_2:String = "";
            while (!this.m_reachedEOF)
            {
                
                _loc_1 = this.getNextChar();
                if (_loc_1 == " " || _loc_1 == "\r" || _loc_1 == "\n" || _loc_1 == "\t")
                {
                    this.skipWhiteSpace();
                    if (_loc_2 != "")
                    {
                        return _loc_2;
                    }
                    continue;
                }
                _loc_2 = _loc_2 + _loc_1;
            }
            return _loc_2;
        }// end function

        public function skipWhiteSpace() : void
        {
            var _loc_1:String = null;
            do
            {
                
                _loc_1 = this.getNextChar();
            }while (_loc_1 == "\n" || _loc_1 == " " || _loc_1 == "\r" || _loc_1 == "\t")
            this.putBack();
            return;
        }// end function

        public function ignoreLine() : void
        {
            var _loc_1:String = null;
            while (!this.m_reachedEOF && _loc_1 != "\n")
            {
                
                _loc_1 = this.getNextChar();
            }
            return;
        }// end function

        public function getNextChar() : String
        {
            var _loc_2:String = this;
            _loc_2.m_parseIndex = this.m_parseIndex + 1;
            var _loc_1:* = this.m_textData.charAt(this.m_parseIndex++);
            if (_loc_1 == "\n")
            {
                var _loc_2:String = this;
                var _loc_3:* = this.m_line + 1;
                _loc_2.m_line = _loc_3;
                this.m_charLineIndex = 0;
            }
            else if (_loc_1 != "\r")
            {
                var _loc_2:String = this;
                var _loc_3:* = this.m_charLineIndex + 1;
                _loc_2.m_charLineIndex = _loc_3;
            }
            if (this.m_parseIndex >= this.m_textData.length)
            {
                this.m_reachedEOF = true;
            }
            return _loc_1;
        }// end function

        public function getNextInt() : int
        {
            var _loc_1:* = parseInt(this.getNextToken());
            if (isNaN(_loc_1))
            {
                this.sendParseError("int type");
            }
            return _loc_1;
        }// end function

        public function getNextNumber() : Number
        {
            var _loc_1:* = parseFloat(this.getNextToken());
            if (isNaN(_loc_1))
            {
                this.sendParseError("float type");
            }
            return _loc_1;
        }// end function

        public function putBack() : void
        {
            var _loc_1:String = this;
            var _loc_2:* = this.m_parseIndex - 1;
            _loc_1.m_parseIndex = _loc_2;
            var _loc_1:String = this;
            var _loc_2:* = this.m_charLineIndex - 1;
            _loc_1.m_charLineIndex = _loc_2;
            this.m_reachedEOF = this.m_parseIndex >= this.m_textData.length;
            return;
        }// end function

        public function parseLiteralString() : String
        {
            this.skipWhiteSpace();
            var _loc_1:* = this.getNextChar();
            var _loc_2:String = "";
            if (_loc_1 != "\"")
            {
                this.sendParseError("\"");
            }
            do
            {
                
                if (this.m_reachedEOF)
                {
                    this.sendEOFError();
                }
                _loc_1 = this.getNextChar();
                if (_loc_1 != "\"")
                {
                    _loc_2 = _loc_2 + _loc_1;
                }
            }while (_loc_1 != "\"")
            return _loc_2;
        }// end function

        private function sendEOFError() : void
        {
            throw new Error("Unexpected end of file");
        }// end function

        private function sendParseError(param1:String) : void
        {
            throw new Error("Unexpected token at line " + (this.m_line + 1) + ", character " + this.m_charLineIndex + ". " + param1 + " expected, but " + this.m_textData.charAt((this.m_parseIndex - 1)) + " encountered");
        }// end function

        private function sendUnknownKeywordError() : void
        {
            throw new Error("Unknown keyword at line " + (this.m_line + 1) + ", character " + this.m_charLineIndex + ". ");
        }// end function

    }
}
